/* vim:ft=objc:
 * $Id: Sound.m 114 2005-05-12 05:15:40Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#import <Cocoa/Cocoa.h>
#import "Sound.h"

#import <MacBiffPlugIn/PreferencesController.h>
#import <MacBiffPlugIn/debug.h>

#define APP_NAME @"MacBiff"
#define PLUGIN_NAME @"Sound"

@implementation Sound

+ (NSString*) plugInName
{
	return PLUGIN_NAME;
}

+ (NSString*) plugInVersion
{
	return @"1.0.0";
}

+ (NSString*) plugInAuthor
{
	return @"Branden J. Moore <bmoore@forkit.org>";
}

+ (NSString*) plugInUrl
{
	return @"http://www.forkit.org/macbiff/";
}


- (id) init
{
	dprintf("Initializing Sound PlugIn.\n");
	if ( ( self = [super init] ) != Nil ) {

		dprintf("Loading Sound's NIB.\n");
		if ( ![NSBundle loadNibNamed: @"Sound" owner: self] ) {
			alert("Unable to load Sound's Nib\n");
			return nil;
		}
	}
	return self;
}


- (void) dealloc
{
	[super dealloc];
}



- (void) setupView
{
	NSArray *soundList = Nil;
	NSDictionary *plugPrefs = Nil;
	BOOL use_beep = YES;
	unsigned int i;

	dprintf("Setting up the View for Sound.\n");

	plugPrefs = [prefs action: PLUGIN_NAME];

	use_beep = [[plugPrefs objectForKey: @"Use System Beep"] boolValue];
	dprintf("I claim that system beep is %s\n", use_beep ? "TRUE" : "FALSE");

	[useBeepButton setState: use_beep ? NSOnState : NSOffState];
	[soundPopUp setEnabled: !use_beep];

	soundList = [self getSounds];
	[soundPopUp removeAllItems];
	for ( i = 0 ; i < [soundList count] ; ++i ) {
		[soundPopUp addItemWithTitle:
			[soundList objectAtIndex: i]];
	}
	[soundPopUp selectItemWithTitle: [plugPrefs objectForKey: @"Sound"]];
	if ( [soundPopUp indexOfSelectedItem] < 0 ) {
		[soundPopUp selectItemAtIndex: 0];
	}
	[soundPopUp synchronizeTitleAndSelectedItem];
}


- (void) preActionForAccount: (Account*)acct
{
	return;
}


- (void) postActionForAccount: (Account*)acct
{
	NSDictionary *plugPrefs = Nil;
	NSArray *newMailFolders = [acct foldersWithNewMailIncludingMasked: NO];

	if ( [newMailFolders count] == 0 ) return;

	plugPrefs = [prefs action: PLUGIN_NAME];

	if ( [[plugPrefs objectForKey: @"Use System Beep"] boolValue] ) {
		NSBeep();
	} else {
		NSSound *sound = [NSSound soundNamed: [plugPrefs objectForKey: @"Sound"]];
		[sound play];
	}

}


- (IBAction) setUseBeep: (id) sender
{
	BOOL use_beep = ([useBeepButton state] == NSOnState);

	[soundPopUp setEnabled: !use_beep];

	[[prefs action: PLUGIN_NAME] setObject:
			[NSNumber numberWithBool: use_beep]
		forKey: @"Use System Beep"];
	[prefs commitActions];
}


- (IBAction) setSound: (id) sender
{
	NSSound *sound = [NSSound soundNamed: [soundPopUp titleOfSelectedItem]];
	[sound play];

	[soundPopUp synchronizeTitleAndSelectedItem];

	[[prefs action: PLUGIN_NAME] setObject:
			[soundPopUp titleOfSelectedItem]
		forKey: @"Sound"];
	[prefs commitActions];

}


- (NSArray*) getSounds
{
	NSSet *aiffSet = [NSSet setWithObject: @"aiff"];
	NSMutableArray *soundNames = [[NSMutableArray alloc]
			initWithCapacity: 15];
	/* Generate listing of all library directories */
	NSArray *tarray = NSSearchPathForDirectoriesInDomains(
			NSLibraryDirectory,
			NSAllDomainsMask,
			YES );
	NSEnumerator *libEnum = [tarray objectEnumerator];
	NSString *libPath = Nil;
	NSDirectoryEnumerator *dirEnum = Nil;
	NSString *fp = Nil;
	NSString *sp = Nil;

	while ( (libPath = [libEnum nextObject]) ) {
		/* Append 'Sounds' to the library path */
		sp = [libPath stringByAppendingFormat: @"/%@", @"Sounds"];
		dirEnum = [[NSFileManager defaultManager]
				enumeratorAtPath: sp];
		while ( (fp = [dirEnum nextObject]) ) {
			if ( [aiffSet containsObject: [fp pathExtension]] ) {
				[soundNames addObject:
					[[[sp stringByAppendingFormat: @"/%@", fp]
						stringByDeletingPathExtension]
							lastPathComponent]];
			}
		}

	}
	return soundNames;
}

@end


